<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>产品配置</el-breadcrumb-item>
        <el-breadcrumb-item>新用户启动跳转(渠道)</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
      >
        <!-- <template slot="menuLeft">
          <el-tag type="danger" ><b> 注意：免广告（天）和免启动弹窗（天）功能只在“橘子猫轻小说1.0.0”以上版本生效</b></el-tag>
        </template>  -->
         <template slot="channel_id" slot-scope="scope">
          <div v-for="(item,index) in scope.row.channel_id" :key="index">{{item}},</div>
        </template> 
        
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { appConfigNewUserStartJump as baseURL,appPopup,selectVersion} from "@/api/cat/baseUrls"; 
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10
      },
      type: true,
      data: [],
      params: {},
      form: {},
      oldForm: {},
      default: {
        orderByList: "version_name&DESC,jump_id&DESC",
      },
      option: {
        border: true,
        selection: true,
border: true,
        tip: true,
        viewBtn: false,
        editBtn: true,
        align: "center",
        headerAlign: "center",
        column: [
          {
            label: "版本号",
            prop: "version_name",
            type: "select",
            search: true,
            placeholder: "请输入版本号",
            dicUrl: `${catUrl}/${selectVersion.selectVersion}`,
            props: {
              label: "app_version",
              value: "app_version",
            },
            dicFormatter: (res) => {
              return res.source;
            },
            rules: [
              {
                required: true,
                message: "版本号必须填写",
              },
            ],
          },
          {
            label: "渠道",
            prop: "channel_id",
            type: "input",
            search: true,
            slot:true,
            rules: [
              {
                required: true,
                message: "渠道号必须填写",
              },
            ],
          },
          {
            label: "跳转页面",
            prop: "jump_id",
            type: "select",
            placeholder: "请选择跳转页面",
            dicData: [],
            props: {
              label: "title",
              value: "id",
            },
            rules: [
              {
                required: true,
                message: "跳转页面必须选择",
              },
            ],
            change: (value) => {
              let data = value.column.dicData.filter(
                (item) => item.id == value.value
              )[0];
              if (data != undefined) {
                setTimeout(() => {
                  this.form.info = data.info; 
                  this.option.column[4].value = data.info;
                  this.option.column[4].addDisplay = true;
                  this.option.column[4].editDisplay = true;  
                }, 1);
              } else {
                this.form.info = "";
                // this.form.jump_url = "";
                this.option.column[4].value= "";
                this.option.column[4].addDisplay = false;
                this.option.column[4].editDisplay = false;
              }
            },
          },
          {
            label: "跳转地址",
            prop: "jump_url",
            type: "input",
            placeholder: "跳转地址"
          },
          {
            label: "",
            prop: "info",
            span: 24,
            tip: "跳转地址说明",
            type: "textarea",
            placeholder: "无跳转地址说明",
            hide: true,
            value:'', 
            addDisplay: false,
            editDisplay: false,
            readonly:true,
          },
          {
            label: "免广告(天)",
            prop: "advertis_free",
            type: "number",
            rules: [
              {
                required: true,
                message: "免广告(天)必须填写",
              },
            ],
          },
          {
            label: "免开屏推荐(天)",
            prop: "popup_free",
            type: "number",
            rules: [
              {
                required: true,
                message: "免开屏推荐(天)必须填写",
              },
            ],
          },
          {
            label: "添加时间",
            prop: "create_time",
            type: "date", 
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
          },
          {
            label: "修改时间",
            prop: "update_time",
            type: "date", 
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
          },
        ]
      }
    };
  },
  methods: {
    /**自定义修改方法,解决跳转路径回显问题 */
    customEdit (scope) {
      console.info('tag', scope)
      this.oldForm = scope.row;
      this.$refs.crud.rowEdit(scope.row, scope.index)
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        `${baseURL.pageAndParam}`,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done,loading) {
      //判断是否填写了跳转路径
      if(row.jump_id==1||row.jump_id==2||row.jump_id==13){
          if(row.jump_url==undefined||row.jump_url==''){
              loading();
              return this.$message.error("请填写跳转路径(参数)");
          }
      }else{
          row.jump_url = "";
      } 
      delete row["id"];
      row.jump_name = row["$jump_id"];
      row.create_time = new Date();

      let userInfo = this.$store.state.user.userInfo;
      row.edit_account_name = userInfo.username;
      row.channel_id = (row.channel_id+ '').split(",")
      let data = await common(`${baseURL.save}`, row, null, "POST");
      if (data == null) {
        loading()
        return;
      }

      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
        //判断是否填写了跳转路径
      if(row.jump_id==1||row.jump_id==2||row.jump_id==13){
        if(row.jump_url==undefined||row.jump_url==''){
            loading();
            return this.$message.error("请填写跳转路径(参数)");
        }
      }else{
          row.jump_url = "";
      }
      row.jump_name = row["$jump_id"];
      let userInfo = this.$store.state.user.userInfo;
      row.edit_account_name = userInfo.username;
      row.channel_id = (row.channel_id+ '').split(",")
      row.edit_time = new Date();
      console.log(Array.isArray(row.channel_id),'channel_id')
      let data = await common(`${baseURL.update}`, row, null, "POST");
      if (data == null) {
        loading()
        return;
      }

      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            `${baseURL.delete}`,
            { id: row.id },
            null,
            "GET"
          );
        })
        .then(() => {
          this.getList();
        });
    },
    /**搜索 */
    searchChange(params, done) {
    //   params.popup_name = `%${params.popup_name ? params.popup_name : ""}%`;
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
  // async created() {
    //查询跳转页面列表
    // let { data } = await getConfigSkippaheType();
    // this.option.column[2].dicData = data.source;
  // },
  
  async created() {
    //查询跳转页面列表
    let {data} = await common(appPopup.getSkippaheType,null,null,"GET"); 
    this.option.column[2].dicData = data;
  },
};
</script>

<style>
</style>